﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Blog_Domain;
using Blog_Domain.Repositories;

namespace BlogWeb.Controllers
{
    public class HomeController : Controller
    {
        private IPostRepository _postRepository;

        public HomeController()
        {
            _postRepository = new PostRepository();
        }

        //
        // GET: /Home/

        public ActionResult Index(int? id)
        {
            return View();
        }

        public ActionResult List()
        {
            var posts = _postRepository.GetAll();
            return View(posts.OrderByDescending(d => d.CreatedDate).ToList());
        }


        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(Post post)
        {
            if (!String.IsNullOrEmpty(post.Title) && !String.IsNullOrEmpty(post.Body))
            {
                post.CreatedDate = DateTime.Now;
                _postRepository.Save(post);

                return RedirectToAction("List");
            }

            return View();
        }

    }
}
